package com.webdrider.servlet.action.user;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.model.abstracts.AbstractRequest;
import com.common.model.abstracts.AbstractValidator;
import com.common.model.validators.ActionValidator;
import com.common.security.abstracts.AbstractError;
import com.common.security.abstracts.AbstractException;
import com.common.security.errors.ParserError;
import com.common.security.exceptions.ManagerException;
import com.common.security.exceptions.ParserException;
import com.common.servlet.abstracts.AbstractParserServlet;
import com.common.servlet.interfaces.IParser;
import com.webdrider.manager.action.user.UserManager;
import com.webdrider.manager.interfaces.IUserManager;
import com.webdrider.model.action.user.User;
import com.webdrider.model.requests.user.AddUserRequest;
import com.webdrider.model.response.user.AddUserResponse;
import com.webdrider.model.validators.AddUserValidator;
import com.webdrider.model.validators.AdminValidator;
import com.webdrider.model.validators.SessionValidator;
import com.webdrider.security.errors.CredentialsError;
import com.webdrider.security.errors.UserError;
import com.webdrider.servlet.util.RequestAction;
import com.webdrider.servlet.util.parser.user.AddUserXMLParser;

public class AddUserServlet extends AbstractParserServlet {
	private static final long serialVersionUID = 1L;
	private static List<AbstractValidator> validators = new ArrayList<AbstractValidator>();

	private AddUserRequest requestContent;
	private AddUserXMLParser parser = new AddUserXMLParser();
	private IUserManager manager = new UserManager();
	
	static {
		validators.add(new ActionValidator(RequestAction.ADD_USER));
		validators.add(new SessionValidator());
		validators.add(new AddUserValidator());
		validators.add(new AdminValidator());
	}

	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response) {
		try {
			if (manager.exist(requestContent.getUsername())) {
				error(new CredentialsError(CredentialsError.USERNAME_EXISTS), response);
			} else {
				User user = new User();
				user.setUsername(requestContent.getUsername());
				user.setPassword(requestContent.getPassword());
				user.setUserType(requestContent.getUserType());
				
				manager.save(user);
				
				AddUserResponse addUserResponse = new AddUserResponse();
				addUserResponse.setRequestAction(requestContent.getAction());
				
				response.setContentType("text/xml");
				response.getWriter().write(parser().parseResponse(addUserResponse));
				response.getWriter().close();
			}
		} catch (IOException ioException) {
			error(new ParserError(ParserError.STREAM_ERROR), response);
		} catch (ManagerException managerException) {
			exception(managerException, response);
		} catch (ParserException parserException) {
			exception(parserException, response);
		}
	}

	@Override
	public void setRequest(AbstractRequest abstractRequest) {
		requestContent = (AddUserRequest) abstractRequest;
	}

	@Override
	public List<AbstractValidator> getValidators() {
		return validators;
	}

	@Override
	public IParser parser() {
		return parser;
	}
	
	@Override
	public void exception(AbstractException exception, HttpServletResponse response) {
		super.exception(exception, response);
		
		switch (exception.getCode()) {
			 case ManagerException.DATABASE_EXCEPTION: {
				 error(new UserError(AbstractError.DATABASE_ERROR), response);
				 break;
			 }
		}
	}
}
